package Change.webapp.action;

import java.util.List;

import Change.model.Interest;
import Change.model.School;
import Change.service.InterestManager;

import com.opensymphony.xwork2.Preparable;

public class InterestAction extends BaseAction implements Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Long id;
	private Interest interest;
	private List<Interest> interests;
	private InterestManager manager;
	private List<Interest> rootInterests;
	private Interest parent;
	
	
	public Long getId() {
		return id;
	}
	public Interest getInterest() {
		return interest;
	}
	public List<Interest> getInterests() {
		return interests;
	}
	public InterestManager getManager() {
		return manager;
	}
	public void setInterestManager(InterestManager manager) {
		this.manager = manager;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public void setInterest(Interest interest) {
		this.interest = interest;
	}
	public void setInterests(List<Interest> interests) {
		this.interests = interests;
	}
	public List<Interest> getRootInterests(){
		return rootInterests;		
	}
	
	public void prepare() throws Exception {
		// nothing to do
		
	}
	
	public String getAllRootInterests(){		
		this.interests = manager.getInterests((Long) null);
		return SUCCESS;
	}
	
	public String getAllInterests(){
		this.interests = manager.getAll();
		return SUCCESS;
	}

	
	public String delete(){
		if(id != null){
			manager.remove(id);
		}else{
			manager.remove(interest.getId());
		}
		
		saveMessage(getText("interest.deleted"));
		return SUCCESS;
	}
	
	public void prepareSave(){
		if(getRequest().getMethod().equalsIgnoreCase("post")){
			if("".equals(getRequest().getParameter("interest.id"))){
				interest = new Interest();
			}else{
				interest = manager.getInterest(
						Long.parseLong(getRequest().getParameter("interest.id")));
			}
		}
	}
	
	public String save(){
		if(null != cancel){
			return CANCEL;
		}
		
		boolean isNew = (null == this.interest.getId());
		interest = manager.save(interest);
		String key = isNew ? "interest.added" : "interest.updated";
		saveMessage(getText(key));
		if(isNew){
			return SUCCESS;
		}else{
			return INPUT;
		}
	}
	
	public void prepareEdit(){
		this.rootInterests = manager.getInterests((Long) null);
	}
	
	public String edit(){
		if(null != id){
			this.interest = manager.getInterest(id);
		}else{
			this.interest = new Interest();
		}
		return SUCCESS;
	}
	

}
